<?php
session_start();
if (isset($_SESSION["account"])) {
?>
<?php require_once('common/header.php'); ?>
<?php require_once('common/sidebar.php'); ?> 
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div class="main-content app-content mt-0">
    <div class="side-app">
        <!--占位-->
<div class="card"> </div>
<!--占位-->
                        <!--查询代理组-->
                          <?php
                                include_once("../untils/conn.php");
                                mysqli_query($con, "set names utf8");
                                $sql = "SELECT group_id, group_name FROM proxy_group";
                                $result = mysqli_query($con, $sql);
                            ?>
        <!-- CONTAINER -->
        <div class="main-container container-fluid">
            <!-- PAGE-HEADER -->
            <div class="row row-sm">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-header">
                        <button type="button" class="btn btn-info-light me-3  mt-2" data-bs-toggle="modal" data-bs-target="#input-modal" data-bs-whatever="@mdo">添加代理</button>
                        <a href="javascript:void(0)" class="btn btn-info-light me-3 mt-2" onclick="copyLink()">邀请代理</a>
                        <h3 class="card-title">我的邀请码：<span id="puid"></span></h3>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-bordered text-nowrap border-bottom" id="responsive-datatable">
                                    <thead>
                                        <tr>
                                            <th class="wd-10p border-bottom-0">代理账号</th>
                                            <th class="wd-10p border-bottom-0">联系人</th>
                                            <th class="wd-10p border-bottom-0">手机号码</th>
                                            <th class="wd-10p border-bottom-0">上级代理</th>
                                            <th class="wd-10p border-bottom-0">是否实名</th>
                                            <th class="wd-10p border-bottom-0">余额</th>
                                            <th class="wd-10p border-bottom-0">状态</th>
                                            <th class="wd-10p border-bottom-0">操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="table-body">
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- CONTAINER CLOSED -->
    </div>
</div>
<!-- Inout modal -->
    <div class="modal fade" id="input-modal">
        <div class="modal-dialog" role="document">
            <div class="modal-content modal-content-demo">
                <div class="modal-header">
                    <h6 class="modal-title">添加代理(暂不可用)</h6>
                    <button class="btn-close" data-bs-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form>
                      <div class="mb-3">
                        <label for="recipient-name" class="col-form-label">登录账号:</label>
                        <input type="text" class="form-control" id="recipient-name">
                      </div>
                      <div class="mb-3">
                        <label for="recipient-name" class="col-form-label">登录密码:</label>
                        <input type="text" class="form-control" id="recipient-name">
                      </div>
                      <div class="mb-3">
                        <label for="recipient-name" class="col-form-label">手机号:</label>
                        <input type="text" class="form-control" id="recipient-name">
                      </div>
                      <div class="mb-3">
                        <label for="recipient-name" class="col-form-label">代理昵称:</label>
                        <input type="text" class="form-control" id="recipient-name">
                      </div>
                      <div class="mb-3">
                        <label for="recipient-name" class="col-form-label">代理实名:</label>
                        <input type="text" class="form-control" id="recipient-name">
                      </div>
                      <div class="mb-3">
                        <label for="recipient-name" class="col-form-label">代理身份证号:</label>
                        <input type="text" class="form-control" id="recipient-name">
                      </div>
                      <div class="mb-3">
                        <label for="recipient-name" class="col-form-label">代理类型:</label>
                        <select class="form-control" id="proxy_type" name="proxy_type" size="1">
                                <option value="0">个人</option>
                                <option value="1">个体/企业</option>
                              </select>
                      </div>
                      <div class="mb-3">
                        <label for="recipient-name" class="col-form-label">代理等级:</label>
                        <select class="form-control" id="group_id" name="group_id" size="1">
                                    <?php while($row = mysqli_fetch_assoc($result)): ?>
                                        <option value="<?php echo $row['group_id']; ?>"><?php echo $row['group_name']; ?></option>
                                    <?php endwhile; ?>
                                </select>
                      </div>
                    </form>
                  </div>
                <div class="modal-footer">
                    <button class="btn ripple btn-success" type="button">提交</button>
                    <button class="btn ripple btn-danger" data-bs-dismiss="modal" type="button">取消</button>
                </div>
            </div>
        </div>
    </div>
    <!-- End Datepicker modal -->
           <?php
        session_start();
        include_once("../untils/conn.php");
        mysqli_query($con, "set names utf8");
        
        // 获取当前正在登录的账号信息的代理
        $proxy_acc = $_SESSION["account"];
        
        // 查询proxy表中的数据
        $query = "SELECT proxy_rename_sta FROM proxy WHERE proxy_acc = '$proxy_acc'";
        $result = mysqli_query($con, $query);
        $row = mysqli_fetch_assoc($result);
        
        if ($row) {
            $proxy_rename_sta = $row['proxy_rename_sta'];
        ?>
        
        <div class="modal fade" id="modaldemo5" data-bs-backdrop="static" data-bs-keyboard="false">
            <div class="modal-dialog modal-dialog-centered text-center" role="document">
                <div class="modal-content tx-size-sm">
                    <div class="modal-body text-center p-4 pb-5">
                        <i class="icon icon-close fs-70 text-danger lh-1 my-5 d-inline-block"></i>
                        <h4 class="text-danger">警告: 您的账号未实名！</h4>
                        <p class="mg-b-20 mg-x-20">平台要求所有代理商必须实名！否则无法使用平台功能！</p>
                        <a href="profile.php" class="btn btn-primary pd-x-25">去实名</a>
                    </div>
                </div>
            </div>
        </div>
        
        
            <!--/div-->
    
<script src="assets/js/jquery.min.js"></script>
<script>
$(document).ready(function() {
  // 发送Ajax请求获取代理数据
  $.ajax({
    url: "controller/Proxylist.php",
    dataType: "json",
    success: function(data) {
      if (data && data.length > 0) {
        // 清空表格内容
        $('#table-body').html('');
        // 渲染代理数据
        $.each(data, function(index, item) {
          var tr = '<tr>' +
            '<td>' + item.account + '</td>' +
            '<td>' + item.rename + '</td>' +
            '<td>' + item.phone + '</td>' +
            '<td>' + item.puid + '</td>' +
            '<td>' + (item.rename_sta == '1' ? '已实名' : '未实名') + '</td>' +
            '<td>￥' + item.balance + '</td>' +
            '<td>' + (item.status == '1' ? '正常' : '冻结') + '</td>' +
            '<td>'+
            '<a href="javascript:void(0)" class="btn btn-primary">佣金设置</a>'+
            '<a href="javascript:void(0)" class="btn btn-info">重置密码</a>'+
            '</td>'+
            '</tr>';
          $('#table-body').append(tr);
        });
      
        
      }
    },
    error: function(error) {
      console.log(error.responseText);
    }
  });
});

function copyLink() {
    var link = window.location.protocol + "//" + window.location.hostname + "/merchant/register.html?puid=" + puid;
    var tempInput = document.createElement("input");
    tempInput.setAttribute("value", link);
    document.body.appendChild(tempInput);
    tempInput.select();
    document.execCommand("copy");
    document.body.removeChild(tempInput);
    alert("复制成功！");
}

var puid = ''; // 定义全局变量puid

$(document).ready(function() {
    $.ajax({
        url: "controller/public/GetPuid.php",
        type: "POST",
        dataType: "json",
        success: function(res) {
            puid = res.puid; // 将获取到的puid保存到全局变量中
            $("#puid").text(puid); // 将puid显示在页面上
        },
        error: function() {
            alert("获取puid失败");
        }
    });
});

</script>

<script>
    $(document).ready(function() {
        <?php
        if ($proxy_rename_sta == 0) {
            // 未实名认证
            echo '$("#modaldemo5").modal("show");';
            echo '$("#modaldemo5").modal({backdrop: "static", keyboard: false});';
        } elseif ($proxy_rename_sta == 1) {
            // 已实名认证
            echo '$("#modaldemo5").modal("hide");';
        }
        ?>
    });
</script>

<?php
} else {
    // 未找到相关数据
    // echo '数据不存在';
}
?>

<?php require_once('common/footer.php'); ?>
<?php
} else {
  echo "<script>alert('登录过过期，没有权限访问该页面');location.href='login.html';</script>";
}
